/*
This source file is subject to the GPLv3 license that is bundled with this
package in the file LICENSE.txt.
It is also available through the world-wide-web at this URL:
http://www.gnu.org/licenses/gpl.txt
*/

/*==============================================================================
  File Name   : TCPClientSocket.cpp
  Description : Implementation file for TCPClientSocket in GNetwork module.

  Created on  : 2009-06-07
  Updated on  : 2009-06-10
  Updated by  : Shiou Ming, Lee

  Cautions    : (important notes)
  ============================================================================*/

//------------------------------------------------------------------------------
//-- INCLUDES & DEFINES
//------------------------------------------------------------------------------
#include "GNetwork.h"
#include <stdio.h>


//------------------------------------------------------------------------------
//-- FUNCTION IMPLEMENTATIONS
//------------------------------------------------------------------------------

TCPClientSocket::TCPClientSocket()
{
    createSocket();
}

TCPClientSocket::TCPClientSocket(const char *serverIP, u_short serverPort)
{
    createSocket();
    setRemoteAddress(serverIP, serverPort);
}

TCPClientSocket::~TCPClientSocket()
{
    printf("Cleaning up TCP Client socket ...\n");

    //-- TODO: closesocket() could be inapplicable, i.e. socket creation failed
    shutdown(clientSocket, 2);
    closesocket(clientSocket);
    WSACleanup();
}

bool TCPClientSocket::createSocket()
{
    printf("Creating TCP Client socket ...\n");

    WSADATA wsaInfo;
    int nError = WSAStartup(SCK_VERSION2_, &wsaInfo);
    if (nError)
    {
        //-- TODO: Err handling for winsock initialization
    }

    clientSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (INVALID_SOCKET == clientSocket)
    {
        //-- TODO: Err handling for socket()
    }

    return false;
}

bool TCPClientSocket::setRemoteAddress(const char *remoteIP, u_short remotePort)
{
    printf("Setting remote address ... [%s:%d]\n", remoteIP, remotePort);

    memset(&serverAddress, 0, sizeof(serverAddress));
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(remotePort);
    serverAddress.sin_addr.s_addr = inet_addr(remoteIP);
    //-- TODO: decide err handling mechanism for setRemoteAddress + add comment

    return false;
}

bool TCPClientSocket::connectServer()
{
    printf("Connecting to TCP server ...\n");

    int result = connect(clientSocket, (sockaddr *) &serverAddress,
            sizeof(serverAddress));

    //-- TODO: decide err handling mechanism for connect()
    if (SOCKET_ERROR == result)
    {

    }

    return false;
}

bool TCPClientSocket::connectServer(const char *remoteIP, u_short remotePort)
{
    setRemoteAddress(remoteIP, remotePort);
    connectServer();

    return false;
}

int TCPClientSocket::sendTextData(const char *textData)
{
    int dataLen = strlen(textData);
    if (NULL == textData || dataLen < 1)
    {
        printf("Error: No text data to be send\n");
        return 0;
    }

    printf("Sending text data (len = %d) ... [%s]\n", dataLen, textData);

    int sentDataLen = 0;
    sentDataLen = send(clientSocket, textData, dataLen, 0);
    if (SOCKET_ERROR == sentDataLen)
    {

    }

    return sentDataLen;
}

int TCPClientSocket::receiveTextData(char *textDataBuffer, u_int bufferLen)
{
    if (NULL == textDataBuffer || bufferLen < 1)
    {
        printf("Error: No buffer for storing received text data\n");
        return 0;
    }

    int receivedDataLen = 0;
    receivedDataLen = recv(clientSocket, textDataBuffer, bufferLen - 1, 0);
    if (SOCKET_ERROR == receivedDataLen)
    {

    }

    printf("Receiving text data (len = %d) ... [%s]\n", receivedDataLen,
            textDataBuffer);

    return receivedDataLen;
}
